# This is an SAM4E-EK board with a single SAM4E16 chip.
# http://www.atmel.com/tools/sam4e-ek.aspx
#
# Tested with OpenOCD 0.8.0

source [find interface/jlink.cfg]

# chip name
set CHIPNAME SAM4E16E

# Versions of OpenOCD after have atmel_sam4e_ek.cfg
# source [find board/atmel_sam4e_ek.cfg]
source [find target/at91sam4sXX.cfg]

# SAM4E chip has only NRST signal
# SAM4E-EK board has only nSRST. nTRST is connected to +3V3 by pull-up.
reset_config srst_only

$_TARGETNAME configure -event gdb-attach {
reset init
}

$_TARGETNAME configure -event reset-init {
sam4e_pll_init
}

$_TARGETNAME configure -event reset-start {
adapter_khz 500
}

$_TARGETNAME configure -event reset-end {
adapter_khz 10000
flash probe 0
}

proc sam4e_pll_init {} {
# Same sequence from ASF/pmc.c

mww 0x400E0A00 0x04000500 ;# FLASH 6 WS

# Initialize main oscillator
mww 0x400E0420 0x00373E09 ;# CKGR_MOR
sleep 10

# Switch to 3-20MHz Xtal oscillator
mww 0x400E0420 0x01373E09 ;# CKGR_MOR
sleep 10

# Disable PLL first
mww 0x400E0428 0x20000000 ;# CKGR_PLLAR
sleep 10

# PLLA 240 MHz
mww 0x400E0428 0x20133F01 ;# CKGR_PLLAR
sleep 10

# PRES CLK_2, MAINCK -> MCK = 120 MHz
mww 0x400E0430 0x00000011 ;# PMC_MCKR
sleep 10

# PRES CLK_2, PLLACK
mww 0x400E0430 0x00000012 ;# PMC_MCKR
sleep 10
}
